<?php
namespace Pay\Controller;
use Think\Controller;

class CommonController extends Controller {

	protected  $user;
	protected function _initialize(){
		$User = A('User');
		$this->user = session($User::SESSION_NAME);

		if( $this->user ){
			//获取用户信息
			$map['A.user_id'] = $this->user['user_id'];
			$db = zyt_data_init();
			$user_acc  = $db->table('User AS A')->field('A.user_name, A.user_phone, A.user_email, B.tong_money_count, B.consumed_money_count')
				->join('user_capital AS B', 'A.user_id', 'B.capital_user_id')
				->where($map)->find();
			$this->user['user_tb'] = floatval(number_format($user_acc['tong_money_count'], 2, '.', ''));
			$this->user['user_ye'] = floatval(number_format($user_acc['consumed_money_count'], 2 , '.', ''));
			$this->user['user_phone'] = $user_acc['user_phone'];
			$this->user['user_email'] = $user_acc['user_email'];
			$this->assign('user', $this->user);
			$this->assign('platform_url', C('SSO_SERVER'));
		}
	}

	protected function chk_login(){
		if( ! $this->user ){
			$this->error('您还没有登录,请先登录');
		}
	}

	//验证支付密码
	protected function _chkPayPwd($pwd, $db){
		if( ! $pwd || ! $this->user )  return false;
		$uid = $this->user['user_id'];
		$pwd = md5($pwd);
		$data = $db->table('User')->where("user_id = $uid AND user_paypassword = '$pwd'")->find();
		return empty($data) ? false : $data['user_paypassword'];
	}
	//支付密码是否为空
	protected function _chkPayPwdEmpty($db,$id){
		$uid = $id;
		$data = $db->table('User')->where("user_id = $uid")->find();
		return $data['user_paypassword'];
	}

	//验证手机验证码
	protected function _chkVerifyCode($code, $db){
		if( ! $code || ! $this->user )  return false;
		$mobile = $this->user['user_phone'];
		$data = $db->field('code_send_time, code_number')->table('send_code')->where("code_bind_number = '$mobile' AND code_number = '$code'")->find();
		if( empty($data) ) return false;
		$C = C('SMS');
		$expired = $C['EXPIRED'] * 60;
		$send_time = $data['code_send_time'];
		if( time() - $send_time > $expired ) return false;
		return $data['code_number'];
	}

	//获取游乐淘平台账号
	protected function getPlatformUserInfo(){
		$db = zyt_data_init();
		$platform = $db->table('platform_info')->field('user_id')->where("platform_code='PF_0000004'")->find();
		$platID   = (int)$platform['user_id'];
		$platTB   = price($db->getUserCapital($platID)[0]['tong_money_count']);
		return array('uid'=>$platID, 'tb'=>$platTB);
	}
}